package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.RequestUtil;
import com.ruoyi.system.domain.Merchant;
import com.ruoyi.system.domain.dto.MerchantDto;
import com.ruoyi.system.mapper.MerchantMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HomestayBasicMapper;
import com.ruoyi.system.domain.entity.HomestayBasic;
import com.ruoyi.system.service.IHomestayBasicService;

/**
 * 民宿基础信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-23
 */
@Slf4j
@Service
public class HomestayBasicServiceImpl extends ServiceImpl<HomestayBasicMapper, HomestayBasic> implements IHomestayBasicService
{
    @Autowired
    private HomestayBasicMapper homestayBasicMapper;

    @Autowired
    MerchantServiceImpl merchantService;
    @Autowired
    MerchantMapper merchantMapper;

    /**
     * 查询民宿基础信息
     * 
     * @param id 民宿基础信息主键
     * @return 民宿基础信息
     */
    @Override
    public HomestayBasic selectHomestayBasicById(Long id)
    {
        return homestayBasicMapper.selectHomestayBasicById(id);
    }

    /**
     * 查询民宿基础信息列表
     * 
     * @param homestayBasic 民宿基础信息
     * @return 民宿基础信息
     */
    @Override
    public List<HomestayBasic> selectHomestayBasicList(HomestayBasic homestayBasic)
    {
        return homestayBasicMapper.selectHomestayBasicList(homestayBasic);
    }

    /**
     * 新增民宿基础信息
     * 
     * @param homestayBasic 民宿基础信息
     * @return 结果
     */
    @Override
    public int insertHomestayBasic(HomestayBasic homestayBasic)
    {
        SysUser user = RequestUtil.getUser();
        homestayBasic.setCreateTime(DateUtils.getNowDate());
        homestayBasic.setCreateBy(user.getNickName());
        homestayBasic.setUserId(user.getUserId());

        // 根据用户id，查找到商家信息
        Merchant merchant = merchantMapper.selectOne(MerchantDto.builder().userId(user.getUserId()).build());
        if (Objects.isNull(merchant)) {
            log.error("关联商家的userId: {}", user.getUserId());
            throw new BaseException("未查询到商家信息");
        }
        homestayBasic.setMerchantId(merchant.getId());
        return homestayBasicMapper.insertHomestayBasic(homestayBasic);
    }

    /**
     * 修改民宿基础信息
     * 
     * @param homestayBasic 民宿基础信息
     * @return 结果
     */
    @Override
    public int updateHomestayBasic(HomestayBasic homestayBasic)
    {
        homestayBasic.setUpdateTime(DateUtils.getNowDate());
        return homestayBasicMapper.updateHomestayBasic(homestayBasic);
    }

    /**
     * 批量删除民宿基础信息
     * 
     * @param ids 需要删除的民宿基础信息主键
     * @return 结果
     */
    @Override
    public int deleteHomestayBasicByIds(Long[] ids)
    {
        return homestayBasicMapper.deleteHomestayBasicByIds(ids);
    }

    /**
     * 删除民宿基础信息信息
     * 
     * @param id 民宿基础信息主键
     * @return 结果
     */
    @Override
    public int deleteHomestayBasicById(Long id)
    {
        return homestayBasicMapper.deleteHomestayBasicById(id);
    }
}
